using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using CKY.AgentPlatform.Users;

namespace CKY.AgentPlatform.EntityFrameworkCore
{
    /// <summary>
    /// 用户相关数据库配置
    /// </summary>
    public static class UserDbContextModelCreatingExtensions
    {
        public static void ConfigureUserManagement(
            this ModelBuilder builder,
            EntityTypeBuilder<PlatformUser> b)
        {
            b.ToTable("PlatformUsers");
            
            // 配置索引
            b.HasIndex(x => x.UserName).IsUnique();
            b.HasIndex(x => x.Email).IsUnique();
            b.HasIndex(x => x.PhoneNumber);
            b.HasIndex(x => x.IsActive);
            b.HasIndex(x => x.IsAdmin);

            // 配置属性
            b.Property(x => x.UserName)
                .IsRequired()
                .HasMaxLength(256);

            b.Property(x => x.Email)
                .IsRequired()
                .HasMaxLength(256);

            b.Property(x => x.Name)
                .IsRequired()
                .HasMaxLength(64);

            b.Property(x => x.Surname)
                .IsRequired()
                .HasMaxLength(64);

            b.Property(x => x.PhoneNumber)
                .HasMaxLength(32);

            b.Property(x => x.AvatarUrl)
                .HasMaxLength(512);

            b.Property(x => x.Department)
                .HasMaxLength(128);

            b.Property(x => x.Position)
                .HasMaxLength(128);

            b.Property(x => x.LastLoginIp)
                .HasMaxLength(45);

            // 配置软删除过滤器
            b.HasQueryFilter(x => !x.IsDeleted);
        }

        public static void ConfigureRoleManagement(
            this ModelBuilder builder,
            EntityTypeBuilder<Role> b)
        {
            b.ToTable("Roles");
            
            // 配置索引
            b.HasIndex(x => x.Name).IsUnique();
            b.HasIndex(x => x.IsDefault);
            b.HasIndex(x => x.Priority);

            // 配置属性
            b.Property(x => x.Name)
                .IsRequired()
                .HasMaxLength(64);

            b.Property(x => x.DisplayName)
                .IsRequired()
                .HasMaxLength(128);

            b.Property(x => x.Description)
                .HasMaxLength(500);

            // 配置JSON转换
            b.Property(x => x.Permissions)
                .HasColumnType("nvarchar(max)");

            // 配置软删除过滤器
            b.HasQueryFilter(x => !x.IsDeleted);
        }

        public static void ConfigureUserRoleManagement(
            this ModelBuilder builder,
            EntityTypeBuilder<UserRole> b)
        {
            b.ToTable("UserRoles");
            
            // 配置复合索引
            b.HasIndex(x => new { x.UserId, x.RoleId }).IsUnique();
            b.HasIndex(x => x.UserId);
            b.HasIndex(x => x.RoleId);
            b.HasIndex(x => x.ExpiresAt);

            // 配置软删除过滤器
            b.HasQueryFilter(x => !x.IsDeleted);
        }

        public static void ConfigureUserManagement(this ModelBuilder builder)
        {
            builder.Entity<PlatformUser>(b =>
            {
                b.ToTable("PlatformUsers");

                // 配置索引
                b.HasIndex(x => x.UserName).IsUnique();
                b.HasIndex(x => x.Email).IsUnique();
                b.HasIndex(x => x.PhoneNumber);
                b.HasIndex(x => x.IsActive);
                b.HasIndex(x => x.IsAdmin);

                // 配置属性
                b.Property(x => x.UserName)
                    .IsRequired()
                    .HasMaxLength(256);

                b.Property(x => x.Email)
                    .IsRequired()
                    .HasMaxLength(256);

                b.Property(x => x.Name)
                    .IsRequired()
                    .HasMaxLength(64);

                b.Property(x => x.Surname)
                    .IsRequired()
                    .HasMaxLength(64);

                b.Property(x => x.PhoneNumber)
                    .HasMaxLength(32);

                b.Property(x => x.AvatarUrl)
                    .HasMaxLength(512);

                b.Property(x => x.Department)
                    .HasMaxLength(128);

                b.Property(x => x.Position)
                    .HasMaxLength(128);

                b.Property(x => x.LastLoginIp)
                    .HasMaxLength(45);

                // 配置软删除过滤器
                b.HasQueryFilter(x => !x.IsDeleted);
            });

            builder.Entity<Role>(b =>
            {
                b.ToTable("Roles");

                // 配置索引
                b.HasIndex(x => x.Name).IsUnique();
                b.HasIndex(x => x.IsDefault);
                b.HasIndex(x => x.Priority);

                // 配置属性
                b.Property(x => x.Name)
                    .IsRequired()
                    .HasMaxLength(64);

                b.Property(x => x.DisplayName)
                    .IsRequired()
                    .HasMaxLength(128);

                b.Property(x => x.Description)
                    .HasMaxLength(500);

                // 配置JSON转换
                b.Property(x => x.Permissions)
                    .HasColumnType("nvarchar(max)");

                // 配置软删除过滤器
                b.HasQueryFilter(x => !x.IsDeleted);
            });

            builder.Entity<UserRole>(b =>
            {
                b.ToTable("UserRoles");

                // 配置复合索引
                b.HasIndex(x => new { x.UserId, x.RoleId }).IsUnique();
                b.HasIndex(x => x.UserId);
                b.HasIndex(x => x.RoleId);
                b.HasIndex(x => x.ExpiresAt);

                // 配置软删除过滤器
                b.HasQueryFilter(x => !x.IsDeleted);
            });
        }
    }
}